package com.dpp.controller.user;

import com.dpp.context.BaseContext;
import com.dpp.dto.EditAddressDTO;
import com.dpp.dto.SaveAddressDTO;
import com.dpp.result.Result;
import com.dpp.service.AddressService;
import com.dpp.vo.AddressInfoVO;
import com.dpp.vo.OutOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/user/user")
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService addressService;
    String regex ="^1[3-9]\\d{9}$|" +
            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
            "^0\\d{3}-\\d{7}$|" +
            "^0\\d{2}-\\d{8}$";
    Pattern pattern = Pattern.compile(regex);

    /**
     * 新增地址
     *
     */
    @PostMapping("/insert")
    @Transactional
    public Result saveAddress(@RequestBody SaveAddressDTO saveAddressDTO){
        try {
            if(saveAddressDTO.getAddress()==null|| saveAddressDTO.getAddress().isEmpty()){
                return Result.error("地址为空");
            }
            if(saveAddressDTO.getPhone()==null|| saveAddressDTO.getPhone().isEmpty()){
                return Result.error("电话为空");
            }
            Matcher matcher = pattern.matcher(saveAddressDTO.getPhone());
            if(!matcher.matches()){
                return Result.error("电话错误");
            }
            //
            saveAddressDTO.setOpenid(BaseContext.getCurrentId());

            return addressService.saveAddress(saveAddressDTO);
        } catch (Exception e) {
            return Result.error("");
        }
    }

    /**
     * 修改默认地址
     *
     * @return
     */
    @Transactional
    @PutMapping("/update/{id}")
    public Result updateDefaultAddress(@PathVariable("id") Integer id){
        if (id==null)
        {
            return Result.error("id为空");
        }
        try {
            return addressService.updateDA(id);
        } catch (Exception e) {
            return Result.error("未知错误");
        }
    }

    /**
     * 编辑地址
     * @return
     */
    @Transactional
    @PutMapping("/saveInfo")
    public Result editAddress(@RequestBody EditAddressDTO editAddressDTO){
        if (editAddressDTO.getId()==null||editAddressDTO.getPhone()==null||editAddressDTO.getAddress()==null)
        {
            return Result.error("");
        }
        Matcher matcher = pattern.matcher(editAddressDTO.getPhone());
        if(!matcher.matches()||editAddressDTO.getId()==null|| editAddressDTO.getAddress().isEmpty()){
            return Result.error("地址信息为空");
        }
        try {
            return addressService.editAddress(editAddressDTO);
        } catch (Exception e) {
            return Result.error("");
        }
    }

    /**
     * 删除地址
     * @return
     */
    @Transactional
    @DeleteMapping("/delete/{id}")
    public Result deleteAddress(@PathVariable("id") Integer id)
    {
        try {
            return addressService.deleteAddress(id);
        } catch (Exception e) {
            return Result.error("");
        }
    }
    /**
     * 查询该用户所有地址
     * @return
     */
    @GetMapping("/getInfo")
    public Result<AddressInfoVO> getInfo()
    {
        AddressInfoVO info = addressService.getInfo(BaseContext.getCurrentId());
        return Result.success(info);
    }

    @GetMapping("/getDA")
    public Result<OutOrderVO> getDA(){
        OutOrderVO outOrderVO = addressService.getDA();
        if (outOrderVO==null)
        {
            return Result.error("无该用户");
        }
        return Result.success(outOrderVO);
    }

}
